package com.zbc.demo.controller;

import com.zbc.demo.enums.GlobalExceptionEnum;
import com.zbc.demo.model.request.LoginReq;
import com.zbc.demo.model.response.Response;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 测试controller
 *
 * @author 万恶的沫白
 * @date 2021/8/13
 */

@RestController
@RequestMapping("/users")
public class UserController extends AbstractController {

    public static final String DEFAULT_USERNAME = "wanedemobai";
    public static final String DEFAULT_PASSWORD = "000000";
    public static final int USERNAME_MIN_LENGTH = 6;

    @PostMapping("/login")
    public Response<?> login(
            @RequestBody LoginReq req
    ) {
        if (req.getUsername().length() < USERNAME_MIN_LENGTH) {
            throw GlobalExceptionEnum.REQUEST_INVALID_PARAM.getException();
        }

        if (!DEFAULT_USERNAME.equals(req.getUsername())) {
            throw GlobalExceptionEnum.NOT_FOUNT.getException();
        }

        if (!DEFAULT_PASSWORD.equals(req.getPassword())) {
            throw GlobalExceptionEnum.PASSWORD_INCORRECT.getException();
        }
        return new Response<>(HttpStatus.OK);
    }
}
